package com.ys.controller.api.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bo.vo.R;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsRole;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.YsRoleService;
import com.ys.vo.LayuiTableVoJA;

@Controller
@RequestMapping("/api/inter/ysRole")
public class ApiInterYsRoleController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<YsRole> commonApiService;
	@Autowired
	private YsRoleService ysRoleService;

	@RequiresPermissions("ysRole:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute YsRole cond
			){
		
		cond.setOrderby("A.CreateTime asc");
		
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@RequiresPermissions("ysRole:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params){
		return ysRoleService.add(params);
	}
	
	/**
	 * 修改
	 */
	@RequiresPermissions("ysRole:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Long id, @RequestParam Map<String, Object> params){
		System.out.println("id---" + id);
		return ysRoleService.edit(id, params);
	}
	
	@RequestMapping("/dtreeForAuth")
    @ResponseBody
	public DtreeSqlDto dtreeForAuth(Model model, HttpServletRequest request, HttpServletResponse response,
			Long roleId){
		
		return ysRoleService.dtreeForAuth(roleId);
	}
	
	@RequestMapping("/listForAuth")
    @ResponseBody
	public R listForAuth(Model model, HttpServletRequest request, HttpServletResponse response,
			Long roleId, Long menuId){
		
		return ysRoleService.listForAuth(roleId, menuId);
	}
	
	@PostMapping("/saveAuth")
	@ResponseBody
	public R saveAuth(HttpServletRequest request, HttpServletResponse response,
			Long roleId,
			String menuArr) {
		
		return ysRoleService.saveAuth(roleId, menuArr);
	}
	
	@PostMapping("/authMenu")
	@ResponseBody
	public R authMenu(HttpServletRequest request, HttpServletResponse response,
			Long roleId,
			Long menuId,
			Integer type,
			Integer isSelected) {
		
		if (isSelected == 1) {
			return ysRoleService.addRoleMenu(roleId, menuId, type);
		} else {
			return ysRoleService.cancelRoleMenu(roleId, menuId);
		}
	}
	
	@PostMapping("/changeAuthMenuType")
	@ResponseBody
	public R changeAuthMenuType(HttpServletRequest request, HttpServletResponse response,
			Long roleId,
			Long menuId,
			Integer type) {

		return ysRoleService.changeAuthMenuType(roleId, menuId, type);
	}
	
	@PostMapping("/authButton")
	@ResponseBody
	public R authButton(HttpServletRequest request, HttpServletResponse response,
			Long roleId,
			Long buttonId,
			Integer isSelected) {
		
		if (isSelected == 1) {
			return ysRoleService.addRoleButton(roleId, buttonId);
		} else {
			return ysRoleService.cancelRoleButton(roleId, buttonId);
		}
	}
	
}
